﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class SelectRadiatorForm : System.Windows.Forms.Form
	{
		public SelectRadiatorForm()
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.InitData();
		}

		private void InitData()
		{
			TreeView allFamilyTreeList = this.sdr.GetAllFamilyTreeList("Radiator");
			this.InitTreeView(allFamilyTreeList);
			if (allFamilyTreeList.Nodes.Count > 0)
			{
				allFamilyTreeList.SelectedNode = TreeViewTools.GetFirstNode(allFamilyTreeList.Nodes[0]);
				TreeViewTools.SetTreeViewFocus(allFamilyTreeList);
				allFamilyTreeList.ExpandAll();
			}
		}

		private void InitTreeView(TreeView tv)
		{
			base.Controls.Add(tv);
			tv.Location = new System.Drawing.Point(12, 12);
			tv.Name = "tvFamilyList";
			tv.Size = new Size(156, 199);
			tv.TabIndex = 0;
			tv.AfterSelect += this.tvFamilyList_AfterSelect;
			tv.NodeMouseClick += this.tvFamilyList_NodeMouseClick;
		}

		private void tvFamilyList_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
		{
		}

		private void tvFamilyList_AfterSelect(object sender, TreeViewEventArgs e)
		{
			TreeNode treeNode = e.Node;
			while (treeNode.Nodes.Count > 1)
			{
				treeNode = treeNode.FirstNode;
			}
			this.lbFamilyType.Items.Clear();
			this.lbFamilyType.Items.AddRange(this.sdr.GetFamilyInfoByParentId("Radiator", int.Parse(treeNode.Tag.ToString())).ToArray());
			this.lbFamilyType.DisplayMember = "Name";
			if (this.lbFamilyType.Items.Count > 0)
			{
				this.lbFamilyType.SelectedItem = this.lbFamilyType.Items[0];
				this.radiatorName.Clear();
				if (treeNode.Parent != null)
				{
					this.radiatorName.Append(treeNode.Parent.Text + "-");
				}
				this.radiatorName.Append(treeNode.Text);
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.familyId = int.Parse((this.lbFamilyType.SelectedItem as ListItem).Tag.ToString());
			string familyNameById = this.sdr.GetFamilyNameById(this.familyId, "Radiator");
			this.familyName.AddRange(familyNameById.Split(new char[]
			{
				';'
			}));
			this.radiatorName.Append("-" + (this.lbFamilyType.SelectedItem as ListItem).Name);
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private SpaceFamilyDataReader sdr = new SpaceFamilyDataReader();

		public List<string> familyName = new List<string>();

		public StringBuilder radiatorName = new StringBuilder();

		public double offset;

		public double wallDistance;

		public int familyId = 1;
	}
}
